<?php
header("Content-Type: text/html; charset=iso-8859-1");
//
//  infos_admin.php
//  vbcll2
//
//  Created by Robin on 2008-06-08.
//  Copyright 2008 Volley-Ball Club Lutry-Lavaux. All rights reserved.
//

require_once('include.php');
$_SESSION['navigation'] = 'infos';
$action = (!empty($_REQUEST['action'])) ? $_REQUEST['action'] : "";
$start = (!empty($_REQUEST['start'])) ? $_REQUEST['start'] : 1;
$id = (!empty($_REQUEST['id'])) ? $_REQUEST['id'] : "";
$_SESSION['id'] = $id;
$_SESSION['cat'] = 'infos';
$user_id = check_cookie();
if($action == 'del'){
	if(!check_admin('commission_presse')) redirect('perso.php?action=admin');
	if($id){
		$info = new info($id);
		echo ($info->delete()) ? "Info effac&eacute;e" : "D&eacute;sol&eacute;, une erreur est survenue";
	}
}
elseif($action == 'new'){
	if(!check_admin('commission_presse') && $id) redirect('perso.php?action=admin');
	echo ($id) ? "<h1>Modifier info</h1>" : "<h1>Nouvelle info</h1>\n";
	$user_id = check_cookie();
	if($id) $info = new info($id);
	$auteur = ($id) ? new membre($info->membre_id) : new membre($user_id);
	echo "<form action = 'infos_admin.php' method = 'post' id = 'myform' name = 'myform'>\n";
		echo leftCol("Titre: ",10);
		$value_titre = ($id) ? $info->titre : "";
		echo rightCol("<input type = 'text' name = 'titre' size = '40' onkeyup = 'writediv(this.value,\"titre_info\")' onblur = 'writediv(this.value,\"titre_info\")' value = \"$value_titre\" />",88);
		$content = "<button type = 'button' name = 'citation' onclick = 'inserttag(\"_\",document.myform.corps);preview(document.myform.corps.value,\"corps_info\");'>_italique_</button>\n";
		$content .= "<button type = 'button' name = 'citation' onclick = 'inserttag(\"*\",document.myform.corps);preview(document.myform.corps.value,\"corps_info\");'>*gras*</button>\n";
		$content .= "<button type = 'button' name = 'couleur' onclick = 'inserttag(\"!!\",document.myform.corps);preview(document.myform.corps.value,\"corps_info\");'>!!couleur!!</button>\n";
		$content .= "<button type = 'button' name = 'citation' onclick = 'insertcode(\" &#39;&#39;Titre&#39;&#39;:http://adresse \",document.myform.corps);preview(document.myform.corps.value,\"corps_info\");'>lien</button>\n";
		echo leftCol("",10);
		echo rightCol($content,88);
		echo leftCol("Corps: ",10);
		$value_corps = ($id) ? $info->corps : "";
		echo rightCol("<textarea name = 'corps' id = 'corps' cols = '40' rows = '20' onkeyup = 'preview(this.value,\"corps_info\");' onblur = 'preview(this.value,\"corps_info\");if(this.value.length < 300)document.myform.small.checked = \"checked\";'>$value_corps</textarea>",88);
		echo leftCol("Courte:",10);
		$checked = ($id) ? (($info->small) ? "checked = 'checked'" : "") : "";
		echo rightCol("<input type = 'checkbox' name = 'small' value = '1' $checked />",88);
		if(check_admin('commission_presse')){
			echo leftCol("Publi&eacute;e:",10);
			$checked = ($id) ? (($info->valide) ? "checked = 'checked'" : "") : "";
			$membre_id = ($id) ? $info->membre_id : $user_id;
			echo rightCol("<input type = 'checkbox' name = 'valide' value = '1' $checked />",88);
			echo leftCol("",10);
			$print = ($id) ? "<input type = 'hidden' name = 'id' value = '$id' /><input type = 'hidden' name = 'auteur_id' value = '$membre_id' />\n" : "";
			$right = "<p class  ='center'>$print<button type = 'button' onclick = 'list_infos()'>annuler</button>&nbsp;\n";
			$right .= "<input type = 'hidden' name = 'action' value = 'add' />\n";
			$right .= "<button type = 'submit' name = 'submit' value = 'add'>enregistrer</button></p>\n";
			echo rightCol($right,88);
		}
		else{
			echo leftCol("",10);
			$right = "<p class  ='center'><input type = 'hidden' name = 'valide' value = '0' />\n";
			$right .= "<input type = 'hidden' name = 'auteur_id' value = '$user_id' />\n";
			$right .= "<button type = 'button' onclick = 'self.location.href=\"index.php\"'>annuler</button>&nbsp;\n";
			$right .= "<input type = 'hidden' name = 'action' value = 'add' />\n";
			$right .= "<button type = 'submit' name = 'submit' value = 'add'>enregistrer</button></p>\n";
			echo rightCol($right,88);
		}
		echo empty_line_two_col();
	echo "</form>\n";
	echo "<div style = 'position: absolute; top: 50px; right: 0px; z-index: 20' id = 'iframe'>\n";
	echo "    <iframe src='img_upload.php?div=info' frameborder='' scrolling='0'></iframe>\n";
	echo "</div>\n";
	echo "<div id='list'></div>\n";
	echo "<div class = 'info'>\n";
	echo "	<div class = 'info_title' id = 'titre_info'>$value_titre</div>\n";
	echo "	<div class = 'info_auteur' id = 'auteur'>par $auteur->prenom $auteur->nom<span class = 'info_date'>".(date('d.m.Y'))."</span></div>\n";
	echo "	<div class = 'info_corps preview' id = 'corps_info'>".(prepare_info($value_corps))."</div>\n";
/*	echo "</div>\n";*/
}
elseif($action == 'list'){
	if(!check_admin('commission_presse')) redirect('perso.php?action=admin');
	echo "<h1>Les infos</h1>\n";
	$q = "select infos.id as id, nom,prenom,infos.titre as titre,datetime,infos.valide as valide from infos left join membres on membre_id = membres.id order by datetime desc";
	$r = ExecRequete($q);
	$max = mysql_num_rows($r);
	$i = 0;
	$fields = array("date","titre","auteur","valide","actions");
	foreach($fields as $field){
		$$field = (!empty($_REQUEST[$field])) ? $_REQUEST[$field] : "";
	}
	echo "<p class = 'center'><button type = 'button' onclick = 'new_info()'><img src = 'images/icon_new.png' alt = '' width = '14' height = '14' />nouvelle info</button></p>\n";

	$table = new table();
	foreach($fields as $field){
		$$field = new table_field($field);
		$$field->print = $field;
		if($field != 'titre' && $field != 'auteur') $$field->styleclass = 'center';
		$table->fields[] = $$field;
		if($field == 'id') $$field->hidden = 1;
	}
	$valide->filter = 0;
	$table->sense = "DESC";
	$table->order =  "date";
	$table->styleclass = '';
	$table->filter = 1;

	while($s = LigneSuivante($r)){
		$row = new table_row();
		foreach($fields as $field){
			if($field == 'titre'){
				$print = (strlen($s->$field) > 50) ? substr($s->$field,0,50)."..." : $s->$field;
			}
			elseif($field == 'date') $print = $s->datetime;
			elseif($field == 'valide') $print = ($s->$field) ? "<span class = 'oui'>oui</span>" : "<span class = 'non'>non</span>";
			elseif($field == 'auteur') $print = $s->prenom." ".(substr($s->nom,0,1)).".";
			elseif($field == 'actions'){
				$print =  "<img src = 'images/icon_edit.png' alt = 'edit' height = '14' width = '14' style = 'cursor:pointer' onclick = 'edit_info($s->id)'\"/>";
				$print .=  "&nbsp;<img src = 'images/icon_delete.png' alt = 'del' height = '14' width = '14' style = 'cursor:pointer' onclick = 'if(confirm(\"Confirmer la suppression de l info\"))del_info($s->id)' />";
			}
			else $print = $s->$field;
			$row->cols[$field] = $print;
		}
		$table->rows[] = $row;
	}
	echo $table->print_header();
	echo $table->print_body();
	echo $table->print_footer();
}
else{
	if(!check_cookie()) redirect('perso.php?action=admin');
	entete('VBCLL - Bienvenue',"","",1);
	if($action == 'add'){
		$corps = (!empty($_REQUEST['corps'])) ? $_REQUEST['corps'] : "";
		$titre = (!empty($_REQUEST['titre'])) ? $_REQUEST['titre'] : "";
		$valide = (!empty($_REQUEST['valide'])) ? $_REQUEST['valide'] : 0;
		$small = (!empty($_REQUEST['small'])) ? $_REQUEST['small'] : 0;
		$membre_id = (!empty($_REQUEST['auteur_id'])) ? $_REQUEST['auteur_id'] : check_cookie();
		if(!check_admin('commission_presse')){
			$valide = 0;
			$id = 0;
			$membre_id = check_cookie();
		}
		$new_info = ($id) ? new info($id) : new info('new');
		$change_date = ($new_info->valide) ? 0 : 1;
		$new_info->corps = $corps;
		$new_info->titre = $titre;
		$new_info->membre_id = $membre_id;
		$new_info->valide = $valide;
		$new_info->small = $small;
		if($id && check_admin('commission_presse')){
			if($change_date && $new_info->valide) $new_info->datetime = "now()";
			if($new_info->update()){
				$texte = "mise &agrave; jour r&eacute;ussie";
			}
			else $texte = "D&eacute;sol&eacute;, une erreur s'est produite";
		}
		else{
			$result = $new_info->insert();
			if($result){
				$texte = $result;
				if(!check_admin('commission_presse')){
					$membre= new membre($user_id);
					$headers =    "MIME-Version: 1.0\r\n" .
					               "Content-type: text/plain; charset=utf-8; format=flowed\r\n" .
					               "Content-Transfer-Encoding: 8bit\r\n" .
					               "From: info@vbcll.ch\r\n" .
					               "X-Mailer: PHP" . phpversion();
					mail("info@vbcll.ch","VBCLL : nouvelle info","$membre->prenom $membre->nom a proposé une info. Veuillez valider",$headers);
				}
				unset($_SESSION['rand']);
			}
			else $texte = "D&eacute;sol&eacute;, une erreur s'est produite";
		}
		echo "<script type = 'text/javascript'>message(\"$texte\")</script>";
	}
	echo "<div id = 'action'></div>\n";
	if(check_admin('commission_presse')){
		if($id) echo "<script type = 'text/javascript'>edit_info($id)</script>\n";
		else echo "<script type = 'text/javascript'>list_infos(1)</script>\n";
	}
	else redirect('perso.php?action=admin');
	pied();
}

?>